<?php

$url = $_GET['url'];


function get_ck(){
    $ch = curl_init();//初始化curl
    $url="https://www.kuaishou.com/";
    curl_setopt($ch, CURLOPT_URL, $url);//设置请求地址
    $header = ["referer:https://www.kuaishou.com/","user-agent:Mozilla/5.0 (Linux; Android 12; 2210132C Build/SP1A.210812.016) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.104 Mobile Safari/537.36"]; //设置一个你的浏览器agent的header
    curl_setopt($ch, CURLOPT_HEADER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置返回结果为字符串
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置请求头部不输出

    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $result = curl_exec($ch);
    preg_match_all('/Set-Cookie:(.*);/iU',$result ,$cookie);
    $ck=$cookie[1][0].";".$cookie[1][1].';';
    curl_close($ch);
    return $ck;
}

function get_location($url){
    for ($i=1; $i<=2; $i++){
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//设置请求地址
        $header = ["user-agent:Mozilla/5.0 (Linux; Android 12; 2210132C Build/SP1A.210812.016) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.104 Mobile Safari/537.36"]; //设置一个你的浏览器agent的header
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置返回结果为字符串
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置请求头部不输出
        curl_exec($ch);//执行curl请求
        $url = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
        curl_close($ch);//关闭curl
    }
    return $url;
}

function convertUrlQuery($query){
    $queryParts = explode('&', $query);
    $params = array();
    foreach ($queryParts as $param)
    {
        $item = explode('=', $param);
        $params[$item[0]] = $item[1];
    }
    return $params;
}

function get_message($url,$cookie){
    $realUrl=get_location($url);
    $arr=parse_url($realUrl);
    $arr=convertUrlQuery($arr["query"]);
    // 写post数据

        
    $json = array("fid" => "0",
             "shareToken" => $arr["shareToken"],
             "shareObjectId" => $arr["shareObjectId"],
             "shareMethod" => "token",
             "shareId" => $arr["shareId"],
             "shareChannel" => "share_copylink",
             "kpn" => "KUAISHOU_VISION",
             "subBiz" => "SINGLE_ROW_WEB",
             "env" => "SHARE_VIEWER_ENV_TX_TRICK",
             "h5Domain" => "m.gifshow.com",
             "photoId" => $arr["shareObjectId"],
             "isLongVideo" => false);
    // 获取视频信息
    $ch = curl_init();
    
    $url="https://m.gifshow.com/rest/wd/photo/info?kpn=KUAISHOU_VISION&captchaToken=";
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json));
    curl_setopt($ch, CURLOPT_URL, $url);  // 设置URL
    curl_setopt($ch, CURLOPT_POST, 1);  // 设置为POST请求
    $header=[
    'Content-Type: application/json',
    'Referer: '.$realUrl,
    'User-Agent: Mozilla/5.0 (Linux; Android 12; 2210132C Build/SP1A.210812.016) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.104 Mobile Safari/537.36'
    ];
    //return $header;
    curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    //curl_setopt($ch, CURLOPT_HEADER, 1);
    $result = curl_exec($ch);
   
    curl_close($ch);
    if (strpos($result,"shareTitle")==false){
        $result=array("code"=>201,"result"=>$result);
        return $result;
}
    $result=urldecode($result);
    $arr=json_decode($result,true);
    $result=array('shareInfo'=>$arr['shareInfo'],'counts'=>$arr['counts'],"url"=>array());
    if (array_key_exists('atlas', $arr)) { 
        $result = array_merge($result, ['type'=>"note"]);
        $cdn="https://".$arr["atlas"]["cdn"][0];
        foreach ($arr["atlas"]["list"] as $i){
            array_push($result["url"], $cdn.$i);
        }
    //为图集
    } 
    else {
        $result = array_merge($result, ['type'=>"video"]);
        array_push($result["url"], $arr['mp4Url']);
    //为视频
    }
    $result = array_merge($result, ["code"=>200]);
    return $result;
}

if (file_exists("./ksck")){
    $ck=file('ksck')[0];
}
else{
    $ck=get_ck();
    file_put_contents("ksck",$ck);
}

if(!isset($url)) {
    $result=array("code"=>202,"result"=>"参数不全");
    $json = json_encode($result,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
    exit($json);
}
$result=get_message($url,$ck);
if ($result["code"]==201) { 
    $ck=get_ck();
    file_put_contents("ksck",$ck);
    $result=get_message($url,$cookie);
}
   $json = json_encode($result,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
   echo $json;

?>